package com.qt.service.oj.impl;

import com.qt.exception.StatusFailException;
import com.qt.manager.oj.LoginManager;
import com.qt.pojo.dto.LoginDTO;
import com.qt.pojo.dto.PostResetPasswordDTO;
import com.qt.pojo.dto.RegisterDTO;
import com.qt.pojo.dto.ResetPasswordDTO;
import com.qt.result.R;

import com.qt.service.oj.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author pgw
 * @version 1.0
 * @description
 * @date Create by 2023/9/18 16:10
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginManager loginManager;

    @Override
    public R login(LoginDTO loginDTO , HttpServletRequest request , HttpServletResponse response) {

        return  R.success(loginManager.login(loginDTO , request , response));

    }


    @Override
    public R getRegisterCode(String email) {

        try {
           return loginManager.getRegisterCode(email);
        } catch (StatusFailException e) {
            return R.error(e.getMessage());
        }

    }

    @Override
    public R register(RegisterDTO registerDto) {
        return  loginManager.register(registerDto);
    }

    @Override
    public R resetPassword(ResetPasswordDTO resetPasswordDto) {
        try {
            loginManager.resetPassword(resetPasswordDto);
            return R.success();
        } catch (StatusFailException e) {
            return R.error(e.getMessage());
        }
    }

    @Override
    public void logout() {
         loginManager.logout();
    }

    @Override
    public R postResetPassword(PostResetPasswordDTO postResetPasswordDto) {
        try {
            loginManager.postResetPassword(postResetPasswordDto);
            return R.success();
        } catch (StatusFailException e) {
            return R.error(e.getMessage());
        }

    }
}
